import { createI18n } from 'vue-i18n'		//引入vue-i18n组件
import messages from './getMessage'
console.info('navigator.language:' + navigator.language)
/**
 * 使用方法:
 *
 * <li>{{ $t('header.home') }}</li>
 * <li>{{ t('layout.header') }}</li>
 * <li>{{ t('layout.header') }}</li>
 * <li>{{ t(`layout.header.${menu.label}`) }}</li>
 */
//注册i8n实例并引入语言文件
const i18n = createI18n( {
  legacy: false, // composition API
  //locale: 'zh_CN',
  //如果本地有语言标识就采用本地，没有就根据浏览器语言默认标识显示语言
  locale: localStorage.getItem('locale') || navigator.language.slice(0, 2),
  globalInjection: true, // 全局注册 $t
  silentTranslationWarn: true, // 消除控制台警告
  silentFallbackWarn: true, // 消除控制台警告
  messages,
  // 设置数字本地化
  numberFormats: {
    'en': {
      currency: { // 添加$
        style: 'currency', currency: 'USD'
      }
    },
    'zh': {
      currency: { // 添加￥
        style: 'currency', currency: 'JPY',currencyDisplay: 'symbol'
      }
    }
  },
  // 设置 日期时间本地化
  datetimeFormats: {
    'en': {
      short: {
        year: 'numeric', month: 'short', day: 'numeric'
      },
      long: {
        year: 'numeric', month: 'short', day: 'numeric',
        weekday: 'short', hour: 'numeric', minute: 'numeric'
      }
    },
    'zh': {
      short: {
        year: 'numeric', month: 'short', day: 'numeric'
      },
      long: {
        year: 'numeric', month: 'short', day: 'numeric',
        weekday: 'short', hour: 'numeric', minute: 'numeric'
      }
    }
  }
})

// setup i18n instance with glob
export function setupI18n(app) {
  app.use(i18n)
}

// 语言切换
export function switchLanguage(index){
  const idx = ['zh','en'][index] || navigator.language.slice(0, 2)
  localStorage.setItem('locale',idx)
  i18n.global.locale = idx
}